<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Cart as CartModel;
use app\common\model\Goodscat;
use app\common\model\Address;
use app\common\model\DeliveryArea;
use app\common\model\DeliveryTime;
use app\common\model\Send;
use app\common\model\Goods;


class Index extends Fornt{


	public function index(){
	  
		update_session(USER_ID);
		clear_catinfocookie();
		parent::wechatJs();
		//$this->redirect('index/delivery'); //
		$this->assign('title', '鲜果屋水果商城');
		return $this->fetch();
	}
	
	
	//测试地理位置授权问题
	public function testdlwzsq(){
	     
	    parent::wechatJs();
	    return $this->fetch();
	}


	/**
	 * 配送入口
	 * @return [type] [description]
	 */
	public function home($model = 1){
	    parent::wechatJs();
		if($model == 2){
			$title = '全市配送';
			$notify='每天1点之前下单送贵阳发货（运费便宜），1点以后联系小编给您协商配送';
			$cartUrl = url('index/cart/delivery');
			$detailUrl = 'goods/qspsdetail';
		}elseif ($model == 21){			
			$title = '中秋专场';
			$notify='金阳多个菜鸟驿站可自提（免运费）';
			$cartUrl = url('index/cart/indexbaoyou');
			$detailUrl = 'goods/jspsdetailbaoyou';
		}elseif ($model == 22){			
			$title = '鲜果商城';
			$notify='下单必看：【石板】石板批发市场发货当天下单第二天任何时候可送达【美团跑腿配送】【龙里】龙里批发市场发货当天11点之前下单当天可以送达【圆通同城物流配送】全场包邮包售后 有特殊时间要求请联系小编15885108322';
			$cartUrl = url('index/cart/index');
			$detailUrl = 'goods/jspsdetail';
		}else {
			$model = 1;
			$title = '专场';
			$notify='';
			$cartUrl = url('index/cart/index');
			$detailUrl = 'goods/jspsdetail';
		}

		//初始化模块信息
		$this->initModel($model);
		//获取模块信息
		$modelinfo = model('Goodsmodel')->find($model);
		//获取分类
		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();
		

		
		//获取购物车数量和价格
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);


		$map['status'] = 1;
		$map['id']= ['in', $modelinfo['goods']];

		//推荐商品
		$map['is_hot'] = 1;
		$TjList = Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();



		$this->assign([
			'Goodscatlist' => $Goodscatlist,
			'TjList' => $TjList,
			'cart' => $cinfo,
			'title' => $title,
			'cartUrl'=> $cartUrl,
			'detailUrl'=> $detailUrl,
		    'notify'=>$notify
		]);
		return $this->fetch('jyps');
	}

// 	/**
// 	 * 配送入口(源文件)
// 	 * @return [type] [description]
// 	 */
// 	public function home($model = 1){
// 	    if($model == 2){
// 	        $title = '全市配送';
// 	        $cartUrl = url('index/cart/delivery');
// 	        $detailUrl = 'goods/qspsdetail';
// 	    }else {
// 	        $model = 1;
// 	        $title = '金阳专送';
// 	        $cartUrl = url('index/cart/index');
// 	        $detailUrl = 'goods/jspsdetail';
// 	    }
	
// 	    //初始化模块信息
// 	    $this->initModel($model);
// 	    //获取模块信息
// 	    $modelinfo = model('Goodsmodel')->find($model);
// 	    //获取分类
// 	    $Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();
// 	    //获取购物车数量和价格
// 	    $cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
	
	
// 	    $map['status'] = 1;
// 	    $map['id']= ['in', $modelinfo['goods']];
	
// 	    //推荐商品
// 	    $map['is_hot'] = 1;
// 	    $TjList = Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();
	
	
	
// 	    $this->assign([
// 	        'Goodscatlist' => $Goodscatlist,
// 	        'TjList' => $TjList,
// 	        'cart' => $cinfo,
// 	        'title' => $title,
// 	        'cartUrl'=> $cartUrl,
// 	        'detailUrl'=> $detailUrl
// 	    ]);
// 	    return $this->fetch('jyps');
// 	}

	public function stindexold(){
		cookie('place_id', 1);
		//设置地址类型 默认为0 普通坐标模式  1为包邮模式
		cookie('address_type', 0);
		cookie('addressbackurl', url('index/cart/index'));
		cookie('goodsmodel_id', 1);
		$map['user_id'] = USER_ID;
		$map['goodsmodel_id'] = ['neq', 1];
		$result = CartModel::destroy($map);
		if($result){
			cookie('deliverytime', null);
		}


        //及时配送
        $modelinfo = model('Goodsmodel')->find(1);

		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->column('*', 'id');
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
		cookie('shopindex', 'index/index/stindex');

		$this->assign('cart', $cinfo);
		$this->assign('cartSum', $cinfo['num']);
		$this->assign('cartCountprice', $cinfo['price']);
		$this->assign('cartGoodId', $cinfo['good_id_arr']);
		$this->assign('cartlist', $cinfo['list']);
		$this->assign('Goodscatlist', $Goodscatlist);
		$this->assign('title', '及时配送');
		return $this->fetch();
	}

	public function stindex($cid = null){
		$this->redirect('index/index/home', ['model'=>1]);
		//初始化模块信息
		$this->initModel(1);
		//获取模块信息
		$modelinfo = model('Goodsmodel')->find(cookie('goodsmodel_id'));

		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();

		$catId = $cid ? $cid : '';

		$map['status'] = 1;
		$map['id']= ['in', $modelinfo['goods']];
		if($catId){
			$map['cat_id'] = $catId;
		}else{
			$map['is_hot'] = 1;
		}
		//全部商品





		//获取配送栏目下的所有商品
		$list  =	Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();
		// //推荐商品
		// $map['is_hot'] = 1;
		// $TjList = Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();



		//获取购物车数量和价格
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);


		$this->assign([
			'cat_id' => $catId,
			'list' => $list,
			'TjList' => $TjList,
			'cart' => $cinfo,
			'title' => '金阳专送',
			'Goodscatlist' => $Goodscatlist
		]);
		return $this->fetch('jsps');
	}





	public function jsps(){
		$this->redirect('index/index/home', ['model'=>1]);
		//初始化模块信息
		$this->initModel(1);
		//获取模块信息
		$modelinfo = model('Goodsmodel')->find(cookie('goodsmodel_id'));
		//获取配送栏目下的所有商品
		$list  =	Goods::where(['status' => 1,'id'=>array('in', $modelinfo['goods'])])->order('cat_sort,update_time desc,id desc')->select();
		//获取购物车数量和价格
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);

		//获取分类
		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();



		$this->assign([
			'list' => $list,
			'cart' => $cinfo,
			'cats' => $Goodscatlist,
			'title' => '金阳专送'
		]);
		return $this->fetch('jsps');
	}

	/**
	 * 金阳配送
	 * @return [type] [description]
	 */
	public function jyps(){
		//初始化模块信息
		$this->initModel(1);
		//获取模块信息
		$modelinfo = model('Goodsmodel')->find(cookie('goodsmodel_id'));
		//获取分类
		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();


		//推荐商品
		$map['is_hot'] = 1;
		$TjList = Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();



		$this->assign([
			'Goodscatlist' => $Goodscatlist,
			'TjList' => $TjList,
			'title' => '金阳专送',
		]);
		return $this->fetch('jyps');
	}


	public function networkindex(){
		cookie('place_id', 2);
		//设置地址类型 默认为0 普通坐标模式  1为包邮模式
		cookie('address_type', 0);
		cookie('addressbackurl', url('index/cart/index'));
		cookie('cart_type', 1);
		$Goodscatlist = Goodscat::where(array('status' => 1,  'goods_type'=>array('in', '1,3')))->order('sort asc,id desc')->column('*', 'id');
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
		cookie('shopindex', 'index/index/networkindex');
		$this->assign('cartSum', $cinfo['num']);
		$this->assign('cartCountprice', $cinfo['price']);
		$this->assign('cartGoodId', $cinfo['good_id_arr']);
		$this->assign('cartlist', $cinfo['list']);
		$this->assign('Goodscatlist', $Goodscatlist);
		$this->assign('title', '贵阳|小河|乌当|新添寨|花溪');
		return $this->fetch();
	}

	public function mailindex(){

		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
		$plist  =	Goods::where(['status' => 1,'vs_type'=>1, 'yd_type'=>0])->select();
		$zlist  =	Goods::where(['status' => 1,'vs_type'=>1, 'yd_type'=>1])->select();

		$this->assign('plist', $plist);
		$this->assign('zlist', $zlist);
		$this->assign('cartSum', $cinfo['num']);
		$this->assign('cartCountprice', $cinfo['price']);
		$this->assign('cartGoodId', $cinfo['good_id_arr']);
		$this->assign('cartlist', $cinfo['list']);

		$this->assign('title', '预约商品');
		return $this->fetch();
	}

	//企业采购
	public function procurement(){
			$this->assign('title', '企业采购');
		return $this->fetch();
	}
	//关于我们
	public function about(){
			$this->assign('title', '关于我们');
		return $this->fetch();
	}

	public function checkout(){
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
		$this->assign('cartSum', $cinfo['num']);
		$this->assign('cartCountprice', $cinfo['price']);
		$this->assign('cartGoodId', $cinfo['good_id_arr']);
		$this->assign('cartlist', $cinfo['list']);
		$this->assign('Goodscatlist', $Goodscatlist);

		$address = Address::get(['user_id'=>USER_ID,'defaults'=>1]);
		$this->assign('address', $address);
		return $this->fetch();
	}


	public function getSecondArea($id = null){
		if (!$id) {
			return $this->error("非法操作！");
		}
		$list = DeliveryArea::all(['first_area'=>$id]);
		return json($list);
	}

	public function getDeliveryTime(){
		$shopindex = cookie('shopindex');
		if($shopindex == 'index/index/stindex'){
			$map['type'] = 0;
  	}elseif ($shopindex == 'index/index/networkindex') {
			$map['type'] = 1;
  	}else{
			$map['type'] = 0;
		}

		$list =  DeliveryTime::where($map)->select();
		return json($list);
	}

	/**
	 * 全市配送
	 * @return [type] [description]
	 */
	public function delivery($cid = null){
		$this->redirect('index/index/home', ['model'=>2]);
		return $this->qsps($cid);
		// $this->initModel(2);
		// //获取购物车数量和价格
		// $cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);
		//
    // $modelinfo = model('Goodsmodel')->find(2);
		// //获取配送栏目下的所有商品
		// $list  =	Goods::where(['status' => 1,'id'=>array('in', $modelinfo['goods'])])->order('cat_sort,update_time desc,id desc')->select();
		//
		// $this->assign([
		// 	'list' => $list,
		// 	'cart' => $cinfo,
		// 	'title' => '全市配送'
		// ]);
		// return $this->fetch();
	}

	public function qsps($cid = null){
		$this->redirect('index/index/home', ['model'=>2]);
		//初始化模块信息
		$this->initModel(2);
		//获取模块信息
		$modelinfo = model('Goodsmodel')->find(2);

		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();

		$catId = $cid ? $cid : '';

		$map['status'] = 1;
		$map['id']= ['in', $modelinfo['goods']];
		if($catId){
			$map['cat_id'] = $catId;
		}else{
			$map['is_hot'] = 1;
		}

		//获取配送栏目下的所有商品
		$list  =	Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();
		//获取购物车数量和价格
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);


		$this->assign([
			'cat_id' => $catId,
			'list' => $list,
			'cart' => $cinfo,
			'title' => '全市配送',
			'Goodscatlist' => $Goodscatlist
		]);
		return $this->fetch('qsps');
	}

	/**
	 * 中秋好礼
	 * @return [type] [description]
	 */
	public function zqhl($cid = null){
	    $this->redirect('index/index/home', ['model'=>21]);
	    //初始化模块信息
	    $this->initModel(2);
	    
	    return $this->fetch('zqhl');
	}
	
	
	/**
	 * 农副产品专场
	 * @return [type] [description]
	 */
	public function nfcpzc($cid = null){
	    $this->redirect('index/index/home', ['model'=>22]);
	    //初始化模块信息
	    $this->initModel(2);
	     
	    return $this->fetch('nfcpzc');
	}
	
	
	/**
	 * 包邮商品
	 */
	 public function frank($cid = null){
		 $modelId = 3;//包邮模块
		 //初始化模块信息
 		$this->initModel($modelId);
		$modelinfo = model('Goodsmodel')->find($modelId);

		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $modelinfo['cats'])))->order('sort asc,id desc')->select();
		$catId = $cid ? $cid : '';

		$map['status'] = 1;
		$map['id']= ['in', $modelinfo['goods']];
		// if($catId){
		// 	$map['cat_id'] = $catId;
		// }else{
		// 	$map['is_hot'] = 1;
		// }

		//获取配送栏目下的所有商品
		$list  =	Goods::where($map)->order('cat_sort,update_time desc,id desc')->select();
		//获取购物车数量和价格
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);


		$this->assign([
			'cat_id' => $catId,
			'list' => $list,
			'cart' => $cinfo,
			'title' => '全省包邮',
			'Goodscatlist' => $Goodscatlist
		]);
		return $this->fetch();

	 }





	/**
	 * 产地直发
	 * @var [type]
	 */
	public function placepinkage(){
		exit();
    cookie('place_id', 2); // 在产地直发买有起作用，防止报错
		cookie('shopindex',null);
		//cookie('address_type', 1);
		//获取购物车数量和价格
		$cinfo = CartModel::getNumAndPrice(['user_id'=>USER_ID]);

		//如果是不同类型的产品清除购物车(通过第一个goods_id中的 vs_type 和 yd_type来判断)
		if($cinfo){
			$firstCartInfo = Goods::get($cinfo['good_id_arr'][0]);
			if($firstCartInfo['vs_type'] != 1 || $firstCartInfo[yd_type] != 1){
				//清除购物车
				$result = CartModel::destroy(['user_id'=>USER_ID]);
			}
		}

		//获取产地直发栏目下的所有商品
		$list  =	Goods::where(['status' => 1,'vs_type'=>1, 'yd_type'=>1])->order('sort,id desc')->select();

		$this->assign([
			'list' => $list,
			'cart' => $cinfo,
			'title' => '产地直发'
		]);
		return $this->fetch();
	}

	public function clearusersession(){
		session('user', null);
		if(!session('user')){
			$this->success('成功');
		}
	}
	/**
	 * 普通模块最新订单（显示七天订单）
	 * @return [type] [description]
	 */
	public function general_neworder(){
	    parent::wechatJs();
	    ///判断是不是管理人员
	    $openid = model('WxUser')->where('id', USER_ID)->value('openid');
	    $info = parent::getWxUserInfo($openid);
	    //0普通用户    3 蔬菜权限用户   6  添加商品用户    9 最高用户
	    $dlproxy = $info['dlproxy'];
	    
			$map['pay'] = ['in', '1,2,3']; //线上付款
			$map['index_type'] = ['in', '0,1']; //普通模块
			$map['buystatus'] = ['gt', '-1']; //正常订单
			
			
			//代理权限大于5的可以看到所有的订单 否则只能看到自己的订单
			if($dlproxy<5){
			    $map['a.dluser_id'] = $info['id'];
			}
		
			if(input('buystatus')==1){
			    $map['a.buystatus'] = 3;
			}
			if(input('buystatus')==2){
			    $map['a.buystatus'] = 1;
			}
			
		
			if(input('remarkbak')){
			    $map['a.nick_name|a.user_name|a.remark|a.address|a.title'] = array('like', '%'.input('remarkbak').'%');
			}
			
			if(input('start') && input('end')){
			    $map['create_time'] = array('between time', [input('start'), input('end')]);
			    $map['buystatus'] = array('gt','0');
			}
			
			//复制条件
			    $mapfh['a.buystatus'] = 1;
			    $mapfh['a.fhway'] = 1;
			
			    //小哥订单复制条件
			    $mapxiaoge['a.buystatus'] = 1;
			    $mapxiaoge['a.fhway'] = 2;
			    
			$date = date('Y-m-d', strtotime('-30 days'));
			
			$list = model('Order')->alias('a')->join('wx_user b','b.id=a.fhuser_id','LEFT')->whereTime('a.create_time', '>=', $date)->where($map)->order(' FIELD(a.buystatus,1,3,0,-1,2,4,5),a.id desc')->field('a.*,b.name')->paginate(10);   // week month
			//邮寄未发货订单
			$listyouji = model('Order')->alias('a')->where($mapfh)->order('a.id asc')->field('a.fhremark,a.remark')->select();   // week month
			//小哥未发货订单
			$listxiaoge = model('Order')->alias('a')->where($mapxiaoge)->order('a.id asc')->field('a.fhremark,a.remark')->select();  // week month
					
			//显示发货人
			$shopsSum = model('Order')->alias('a')->where($map)->sum('shop_price');
			$dlpritotalSum = model('Order')->alias('a')->where($map)->sum('dlshop_price');
			
			$this->assign('list', $list);
			$this->assign('listyouji', $listyouji);
			$this->assign('listxiaoge', $listxiaoge);
			$this->assign('shopsSum', $shopsSum);
			$this->assign('dlpritotalSum',$dlpritotalSum);
			$this->assign('dlproxy',$dlproxy);
			return $this->fetch('neworder');
	}
	
	
	/**
	 * 普通模块最新订单（显示七天订单）
	 * @return [type] [description]
	 */
	public function geordertest(){
	    $map['pay'] = ['in', '1,2,3']; //线上付款
	    $map['index_type'] = ['in', '0,1']; //普通模块
	    $map['buystatus'] = ['gt', '-1']; //正常订单
	    	
	
	
	
	    if(input('remarkbak')){
	        $map['a.nick_name|a.user_name|a.remark|a.address|a.title'] = array('like', '%'.input('remarkbak').'%');
	    }
	    	
	    if(input('start') && input('end')){
	        $map['create_time'] = array('between time', [input('start'), input('end')]);
	        $map['buystatus'] = array('gt','0');
	    }
	    	
	    	
	    	
	    $date = date('Y-m-d', strtotime('-30 days'));
	    $list = model('Order')->alias('a')->join('wx_user b','a.fhuser_id=b.id','LEFT')->whereTime('a.create_time', '>=', $date)->where($map)->order('a.id desc')->field('a.*,b.name')->paginate(10);   // week month
	    //显示发货人
	    $shopsSum = model('Order')->alias('a')->where($map)->sum('shop_price');
	    $dlpritotalSum = model('Order')->alias('a')->where($map)->sum('dlshop_price');
	    	
	    $this->assign('list', $list);
	    $this->assign('shopsSum', $shopsSum);
	    $this->assign('dlpritotalSum',$dlpritotalSum);
	    	
	    return $this->fetch();
	}
	
	
	
	public function place_neworder(){
			$map['pay'] = ['in', '1,2,3']; //线上付款
			$map['index_type'] = 2; //普通模块
			$date = date('Y-m-d', strtotime('-30 days'));
			$list = model('Order')->whereTime('create_time', '>=', $date)->where($map)->order('id desc')->paginate(10);   // week month
			$this->assign('list', $list);
			return $this->fetch();
	}


	public function getSubscribe(){
		$openid = model('WxUser')->where('id', USER_ID)->value('openid');
		$info = parent::getWxUserInfo($openid);
		//$re['value'] = $info['subscribe'];
		//不进行判断
		$re['value'] = 1;
		return json($re);
	}

	public function goods_list(){
		$map = ['status'=>1, 'vs_type'=>1, 'yd_type'=>0];
		$list  = Goods::getPaginateBymap($map);
		$this->assign('list', $list);
		return $this->fetch();
	}


	public function qiye2($cid = null, $catId =null){

		$info = parent::setQiyeCommon();

		//获取该企业的分类和商品信息
		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $info['modelInfo']['cats'])))->order('sort asc,id desc')->column('*', 'id');

		$this->assign('cid', $cid);
		$this->assign('cart', $info['cinfo']);
		$this->assign('cartSum', $info['cinfo']['num']);
		$this->assign('cartCountprice', $info['cinfo']['price']);
		$this->assign('cartGoodId', $info['cinfo']['good_id_arr']);
		$this->assign('cartlist', $info['cinfo']['list']);
		$this->assign('Goodscatlist', $Goodscatlist);
		$this->assign('companyInfo',$info['companyInfo']);
		return $this->fetch();
	}


	public function qiye($cid = null,$catid =null){
		$info = parent::setQiyeCommon();
		parent::wechatJs();

		$Goodscatlist = Goodscat::where(array('status' => 1,  'id'=>array('in', $info['modelInfo']['cats'])))->order('sort asc,id desc')->select();

		$map['status'] = 1;
		$map['id']= ['in', $info['modelInfo']['goods']];

		if($catid){
			$map['cat_id'] = $catid;
		}else{
			//$map['cat_id'] = $Goodscatlist[0]['id'];
			$map['is_hot|is_timelimit'] = 1;
		}

		//获取配送栏目下的所有商品
		$list  =	Goods::where($map)->order('is_timelimit desc,cat_sort,update_time desc,id desc')->select();






		$this->assign('companyInfo',$info['companyInfo']);
		$this->assign([
			'cat_id' => $catid,
			'list' => $list,
			'cart' => $info['cinfo'],
			'Goodscatlist' => $Goodscatlist
		]);
		return $this->fetch('qiye2');


	}

	public function grouponlist(){
		//读取企业列表
		$map['displaylist'] = 1;
		$map['status'] = 1;
		$list = model('company')->where($map)->select();
		parent::wechatJs();
		$this->assign('list', $list);
		$this->assign('title', '限时团购');
		return $this->fetch();
	}


}
